package com.fedming.bottomnavigationdemo;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class HiddenSettingLoginActivity extends AppCompatActivity {

    private Button login;
    private ImageView exit;
    private EditText name;
    private EditText password;
    private ImageView clear_name;
    private ImageView clear_password;
    private LinearLayout mLlLoginUsername;
    private LinearLayout mLlLoginPwd;





    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        hideBottomUIMenu();
        //getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.hidden_setting_login);
        login = (Button) findViewById(R.id.bt_login_submit);
        exit = (ImageView) findViewById(R.id.ib_navigation_back);
        name = (EditText) findViewById(R.id.et_login_username);
        password = (EditText) findViewById(R.id.et_login_pwd);
        clear_name = (ImageView) findViewById(R.id.iv_login_username_del);
        clear_password = (ImageView) findViewById(R.id.iv_login_pwd_del);

        mLlLoginUsername = (LinearLayout) findViewById(R.id.ll_login_username);
        mLlLoginPwd =(LinearLayout) findViewById(R.id.ll_login_pwd);


        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                JSONObject settingJson = JSONObject.parseObject(load("setting"));
                //先验证密码

                // 直接传入JSONObject来构造一个实例
                if(password.getText().toString().equals(settingJson.getString("password")) && name.getText().toString().equals(settingJson.getString("administer"))){
                    //打开设置界面活动
                    Intent intent = new Intent(HiddenSettingLoginActivity.this, SettingActivity.class);
                    //intent.putExtra("ServerAddr", addr);
                    //intent.putExtra("userid", userId);
                    HiddenSettingLoginActivity.this.startActivity(intent);
                }
                else {
                    AlertDialog.Builder alertbuilder = new AlertDialog.Builder(HiddenSettingLoginActivity.this);
                    alertbuilder.setTitle("提示");
                    alertbuilder.setMessage("账号或密码错误，请重新输入");
                    alertbuilder.setCancelable(false);
                    alertbuilder.setPositiveButton("确认",null);
                    alertbuilder.show();
                }


            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //返回
                finish();
            }
        });
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password.clearFocus();
                name.setFocusableInTouchMode(true);
                name.requestFocus();
            }
        });
        //设置是否为选中，从而改变editext的外观
        name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mLlLoginUsername.setActivated(true);
                    mLlLoginPwd.setActivated(false);
                }
            }
        });

        name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override      //如果name不为空，则显示清除按钮
            public void afterTextChanged(Editable editable) {
                String username = name.getText().toString().trim();
                if (username.length() > 0) {
                    clear_name.setVisibility(View.VISIBLE);
                } else {
                    clear_name.setVisibility(View.INVISIBLE);
                }
            }
        });

        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name.clearFocus();
                password.setFocusableInTouchMode(true);
                password.requestFocus();
            }
        });
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    mLlLoginPwd.setActivated(true);
                    mLlLoginUsername.setActivated(false);
                }
            }
        });
        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override //如果password不为空，则显示清除按钮
            public void afterTextChanged(Editable editable) {
                String pwd = password.getText().toString().trim();
                if (pwd.length() > 0) {
                    clear_password.setVisibility(View.VISIBLE);
                } else {
                    clear_password.setVisibility(View.INVISIBLE);
                }

            }
        });

        clear_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                name.setText(null);
            }
        });
        clear_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                password.setText(null);
            }
        });

    }
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {

            Window _window = getWindow();
            WindowManager.LayoutParams params = _window.getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
            _window.setAttributes(params);
        }
    }

    private String load(String file_name) {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder content = new StringBuilder();
        try {
            in = openFileInput(file_name);
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return content.toString();
    }

}
